package com.chengwang.bookstory.controller;

import com.chengwang.bookstory.pojo.Book;
import com.chengwang.bookstory.pojo.MyShop;
import com.chengwang.bookstory.pojo.ShopList;
import com.chengwang.bookstory.pojo.User;
import com.chengwang.bookstory.service.BookService;
import com.chengwang.bookstory.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("shop")
public class shopController {
    @Autowired
    private ShopService shopService;
    @Autowired
    private BookService bookService;
    //加入购物车
    @RequestMapping("/add")
    public ModelAndView add(HttpServletRequest request){
        ModelAndView mv=new ModelAndView();
        String bookId = request.getParameter("bookId");
        System.out.println(bookId);
        String shopNumber = request.getParameter("shopNumber");
        System.out.println(shopNumber);
        int shopNumber1 = Integer.parseInt(shopNumber);

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        System.out.println(user);
        if (user==null){
            mv.setViewName("redirect:/login.html");
            return mv;
        }else {
        shopService.add(user.getUserAccount(),bookId,shopNumber1);
            String searchMessage = (String) session.getAttribute("searchMessage");
            List<Book> bookList=bookService.queryByAnyType(searchMessage);
            mv.addObject("bookList",bookList);
            mv.setViewName("search.html");
        return mv;
        }
    }
    //查询购物车
    @RequestMapping("/queryAllMyShop")
    public ModelAndView queryAllShop(HttpSession session){
        ModelAndView mv=new ModelAndView();
        User user = (User) session.getAttribute("user");
        if(user==null){
            mv.setViewName("redirect:/login.html");
            return mv;
        }
        List<MyShop> list=shopService.queryAllMyShop(user.getUserAccount());
        mv.addObject("myShopList",list);
        mv.setViewName("myshop.html");
        return mv;
    }
    //清空
    @RequestMapping("/deleteAll")
    public ModelAndView deleteAll(HttpSession session){
        ModelAndView mv=new ModelAndView();
        User user = (User) session.getAttribute("user");
        shopService.deleteAll(user.getUserAccount());
        List<MyShop> myShopList = shopService.queryAllMyShop(user.getUserAccount());
        mv.addObject("myShopList",myShopList);
        mv.setViewName("myshop.html");
        return mv;
    }

    //删除指定购物单
    @RequestMapping("/delById")
    public ModelAndView delById(HttpServletRequest request,HttpSession session){
        ModelAndView mv=new ModelAndView();
        String bookId = request.getParameter("bookId");
        String time = request.getParameter("time");
        User user = (User) session.getAttribute("user");
        shopService.delById(user.getUserAccount(),bookId,time);
        List<MyShop> myShops = shopService.queryAllMyShop(user.getUserAccount());
        mv.addObject("myShopList",myShops);
        mv.setViewName("myshop.html");
        return mv;
    }
}
